
const config = require('../config').wechat;
const request = require('request');
const codefix = 'https://api.weixin.qq.com/sns/jscode2session?';


function getOpenid  (code) {//会返回一个url

    var appID = config.appID
    var appSecret = config.appSecret

    return new Promise(function (resolve, reject) {

        var url = codefix + 'appid=' + appID + '&secret=' + appSecret + '&js_code=' + code + '&grant_type=authorization_code'

        //console.log(url)
        request(url, function (error, response, body) {
            if (!error && response.statusCode == 200) {
                resolve(body)
            }
        })

    })
}



module.exports = getOpenid